package com.cote.douhahou.pretloan.utils;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.cote.douhahou.pretloan.R;

public class PhotoAkomapalendSelectorDialog extends Dialog {

    private final Runnable runnableByTakePhoto, runnableByFromAlbum;

    private View take_akomapalendPhotoView, from_akomapalendAlbum_akomapalendView;
    private ImageView take_akomapalendPhoto_akomapalendIv, from_akomapalendAlbumIv;
    private TextView take_akomapalendPhoto_akomapalendTv, from_akomapalendAlbum_akomapalendTv;
    private ImageView take_akomapalendPhoto_akomapalendSelect_akomapalendIv, from_akomapalendAlbum_akomapalendSelectIv;

    private boolean is_akomapalendTake_akomapalendPhoto = true;

    public PhotoAkomapalendSelectorDialog(@NonNull Context context, Runnable takeAPhotoRunnable, Runnable fromAlbumRunnable) {
        super(context);
        this.runnableByTakePhoto = takeAPhotoRunnable;
        this.runnableByFromAlbum = fromAlbumRunnable;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_photo);

        getWindow().setBackgroundDrawableResource(R.color.transparent);
        setCanceledOnTouchOutside(true);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        take_akomapalendPhotoView = findViewById(R.id.dpsa_view_takephoto);
        from_akomapalendAlbum_akomapalendView = findViewById(R.id.dpsa_view_fromalbum);
        take_akomapalendPhoto_akomapalendIv = findViewById(R.id.dpsa_iv_takephoto);
        from_akomapalendAlbumIv = findViewById(R.id.dpsa_iv_fromalbum);
        take_akomapalendPhoto_akomapalendTv = findViewById(R.id.dpsa_tv_takephoto);
        from_akomapalendAlbum_akomapalendTv = findViewById(R.id.dpsa_tv_fromalbum);
        take_akomapalendPhoto_akomapalendSelect_akomapalendIv = findViewById(R.id.dpsa_iv_takephoto_select);
        from_akomapalendAlbum_akomapalendSelectIv = findViewById(R.id.dpsa_iv_fromalbum_select);

        init_akomapalendView();
    }

    @SuppressLint({"SetTextI18n", "ClickableViewAccessibility"})
    private void init_akomapalendView(){

        findViewById(R.id.dpsa_view_close).setOnClickListener(v -> dismiss());

        findViewById(R.id.dpsa_view_bg).setOnTouchListener((v, event) -> true);

        take_akomapalendPhotoView.setOnClickListener(v -> take_akomapalendPhoto());
        from_akomapalendAlbum_akomapalendView.setOnClickListener(v -> from_akomapalendAlbum());

        findViewById(R.id.dpsa_btn_confirm).setOnClickListener(v -> {
            if (is_akomapalendTake_akomapalendPhoto){
                if (runnableByTakePhoto !=null) runnableByTakePhoto.run();
                dismiss();
            }else {
                if (runnableByFromAlbum !=null) runnableByFromAlbum.run();
                dismiss();
            }
        });

    }

    private void take_akomapalendPhoto(){
        is_akomapalendTake_akomapalendPhoto = true;
        take_akomapalendPhotoView.setBackgroundResource(R.drawable.back_border_purple_2);
        take_akomapalendPhoto_akomapalendIv.setImageResource(R.drawable.image_camera_3);
        take_akomapalendPhoto_akomapalendTv.setTextColor(getContext().getColor(R.color.word_light));
        take_akomapalendPhoto_akomapalendSelect_akomapalendIv.setBackgroundResource(R.drawable.icon_check10);
        from_akomapalendAlbum_akomapalendView.setBackgroundResource(R.drawable.back_gray_round_c_2);
        from_akomapalendAlbumIv.setImageResource(R.drawable.image_image);
        from_akomapalendAlbum_akomapalendTv.setTextColor(getContext().getColor(R.color.word_gray));
        from_akomapalendAlbum_akomapalendSelectIv.setBackgroundResource(R.drawable.icon_no_check_3);

    }

    private void from_akomapalendAlbum(){
        is_akomapalendTake_akomapalendPhoto = false;

        take_akomapalendPhotoView.setBackgroundResource(R.drawable.back_gray_round_c_2);
        take_akomapalendPhoto_akomapalendIv.setImageResource(R.drawable.icon_camera);
        take_akomapalendPhoto_akomapalendTv.setTextColor(getContext().getColor(R.color.word_gray));
        take_akomapalendPhoto_akomapalendSelect_akomapalendIv.setBackgroundResource(R.drawable.icon_no_check_3);
        from_akomapalendAlbum_akomapalendView.setBackgroundResource(R.drawable.back_border_purple_2);
        from_akomapalendAlbumIv.setImageResource(R.drawable.image_image_p);
        from_akomapalendAlbum_akomapalendTv.setTextColor(getContext().getColor(R.color.word_light));
        from_akomapalendAlbum_akomapalendSelectIv.setBackgroundResource(R.drawable.icon_check10);

    }

}
